package xuhaijia.lanou3g.autohome.findcar.brand;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.StringValues;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.custom.PinnedHeaderListView;
import xuhaijia.lanou3g.autohome.custom.ToastCustom;
import xuhaijia.lanou3g.autohome.forum.normalforum.OrderAdapter;
import xuhaijia.lanou3g.autohome.myself.CollectionActivity;
import xuhaijia.lanou3g.autohome.myself.HistoryActivity;

/**
 * Created by 7 on 16/8/16.
 */
public class BrandFragment extends BaseFragment implements View.OnClickListener {

    private LinearLayout myCollection;
    private LinearLayout sawHistory;
    private LinearLayout hotRank;
    private RecyclerView hotBrandRv;
    private HotBrandAdapter hotBrandAdapter;
    String[] brandOrder = StringValues.brandOrder;
    private ListView brandOrderBrandLv;
    private OrderAdapter brandOrderAdapter;
    private BrandNameAdapter brandNameAdapter;
    private PinnedHeaderListView phlv;
    private ArrayList<ArrayList<String>> name;
    private String[] letterToast = StringValues.brandOrderToast;

    private ArrayList<HashMap<String, String>> type;
    private ToastCustom toastCustom;
    private Intent intent;

    @Override
    protected int setLayout() {
        return R.layout.fragment_brand;
    }

    @Override
    protected void initView() {
        brandOrderBrandLv = bindView(R.id.brand_order_lv);
        brandOrderBrandLv.bringToFront();
        phlv = bindView(R.id.brand_goods_phlv);
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_brand_header,phlv, false);
        View xingView = LayoutInflater.from(mContext).inflate(R.layout.fragment_order_header, brandOrderBrandLv , false);
        myCollection = (LinearLayout) view.findViewById(R.id.brand_mycollection);
        sawHistory = (LinearLayout) view.findViewById(R.id.brand_sawhistory);
        hotRank = (LinearLayout) view.findViewById(R.id.brand_hotrank);
        hotBrandRv = (RecyclerView) view.findViewById(R.id.brand_hotbrand_rv);
        brandOrderBrandLv.addHeaderView(xingView);
        phlv.addHeaderView(view);
        myCollection.setOnClickListener(this);
        sawHistory.setOnClickListener(this);

    }

    @Override
    protected void initData() {
//        toastCustom = new ToastCustom();
        brandOrderAdapter = new OrderAdapter(mContext);
        brandOrderAdapter.setOrder(brandOrder);
        brandOrderBrandLv.setAdapter(brandOrderAdapter);
        GridLayoutManager hotBrandManager = new GridLayoutManager(mContext, 5);
        hotBrandRv.setLayoutManager(hotBrandManager);
        hotBrandAdapter = new HotBrandAdapter(mContext);
        mNetTool.getNetData(URLValues.HOT_BRAND_URL, HotBrandBean.class, new NetTool.NetListener<HotBrandBean>() {
            @Override
            public void onSuccess(HotBrandBean hotBrandBean) {
                hotBrandAdapter.setHotBrandBean(hotBrandBean);
                hotBrandRv.setAdapter(hotBrandAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        brandNameAdapter = new BrandNameAdapter(mContext);
        mNetTool.getNetData(URLValues.ALL_BRAND_URL, BrandNameBean.class, new NetTool.NetListener<BrandNameBean>() {
            @Override
            public void onSuccess(BrandNameBean brandNameBean) {
                name = new ArrayList<ArrayList<String>>();
                ArrayList<String> a = new ArrayList<String>();
                type = new ArrayList<HashMap<String, String>>();
                HashMap<String, String> b = new HashMap<String, String>();
                for (int i = 0; i < brandNameBean.getResult().getBrandlist().size(); i++) {
                    for (int n = 0; n < brandNameBean.getResult().getBrandlist().get(i).getList().size(); n++) {
                        a.add(brandNameBean.getResult().getBrandlist().get(i).getList().get(n).getName());
                        b.put(brandNameBean.getResult().getBrandlist().get(i).getList().get(n).getName()
                                , brandNameBean.getResult().getBrandlist().get(i).getList().get(n).getImgurl());
                    }
                    name.add(a);
                    type.add(b);
                    a = new ArrayList<String>();
                    b = new HashMap<String, String>();
                }
                brandNameAdapter.setName(name);
                brandNameAdapter.setType(type);
                phlv.setAdapter(brandNameAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        brandOrderBrandLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int rightSection = 0;
                if (position < 2) {
                    rightSection = position;
                } else {
                    for (int i = 0; i < position - 1; i++) {
                        rightSection += brandNameAdapter.getCountForSection(i) + 1;
                    }
                    rightSection++;
                }
                phlv.setSelection(rightSection);
                Toast toast = new Toast(mContext);
                toast.cancel();
//                toastCustom.getToast(getActivity(), letterToast[position]);
                ToastCustom.showText(getContext(),letterToast[position]);
            }
        });

    }

    @Override
    public void load() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.brand_mycollection:
                intent = new Intent(getContext(), CollectionActivity.class);
                startActivity(intent);
                break;
            case R.id.brand_sawhistory:
                intent = new Intent(getContext(), HistoryActivity.class);
                startActivity(intent);
        }
    }
}
